package com.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.helper.DBHelper;
import com.model.QuestionDiscuss;

public class QuestionDiscussDao {
	
	private QuestionDiscuss toQuestionDiscuss(Map<String, Object> data) 
	{
		QuestionDiscuss q = new QuestionDiscuss();
		q.setId((Integer)data.get("id"));
		q.setContent((String)data.get("content"));
		return q;
	}
	
	
	public List<QuestionDiscuss> getAll() 
	{
		List<QuestionDiscuss> result = new ArrayList<>();
		String sql = "select * from que_discuss";
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result.add(toQuestionDiscuss(ele));
		}
		return result;
	}

	public Boolean add(String cont) {
		String sql = "INSERT INTO `que_discuss` (`content`) VALUES ('" + cont +"')";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}


	public QuestionDiscuss getById(Integer queId) {
		QuestionDiscuss result = new QuestionDiscuss();
		String sql = "select * from que_discuss where id=" + queId;
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result = toQuestionDiscuss(ele);
		}
		return result;
	}

	public boolean edit(int qid, String cont) {
		String sql = "UPDATE `que_discuss` SET `content` = ? WHERE `id` = ?;";
		List<Object> param = new ArrayList<>();
		param.add(cont);
		param.add(qid);
		DBHelper db = new DBHelper();
		return db.update(sql, param);
	}

}
